﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Wicresoft.Common;
using System.Text;

public partial class Page_SignStat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindData();
    }

    private void BindData()
    {
        string where = "1=1";
        if (tbStart.Text != string.Empty)
            where += " and  SignTime >'" + tbStart.Text + "'";
        if (tbEnd.Text != string.Empty)
            where += " and SignTime <'" + DateTime.Parse(tbEnd.Text).AddDays(1).ToString("yyyy-MM-dd") + "'";

        int count = 0;
        DataTable dt = DataAccess.Select(typeof(AttendenceSign), pager1.PageIndex, pager1.PageSize, where, "SignTime desc", ref count);
        rpList.DataSource = dt;
        rpList.DataBind();
        pager1.RecordCount = count;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        pager1.PageIndex = 0;
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        string where = "1=1";
        if (tbStart.Text != string.Empty)
            where += " and  SignTime >'" + tbStart.Text + "'";
        if (tbEnd.Text != string.Empty)
            where += " and SignTime <'" + DateTime.Parse(tbEnd.Text).AddDays(1).ToString("yyyy-MM-dd") + "'";


        DataTable dt = DataAccess.Select(typeof(AttendenceSign), where, "SignTime desc");


        string filename = "签到明细统计";
        if (tbStart.Text != string.Empty)
        {
            filename += Convert.ToDateTime(tbStart.Text).ToString("(yyyy年MM月dd日");
        }
        else
        {
            filename += "";
        }
        if (tbEnd.Text != string.Empty)
        {
            filename += "-" + Convert.ToDateTime(tbEnd.Text).ToString("yyyy年MM月dd日)");
        }
        else
        {
            filename += "";
        }
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(filename) + ".xls");
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

        StringBuilder sw = new StringBuilder();
        sw.Append("<table border='1' style='border-collapse:collapse'>");
        string tableHeader = @"<tr class=""Normal"">
                                    <td width=""120"" align=""center""  >
                                        序号
                                    </td>
                                    <td width=""120"" align=""center""  >
                                        部门
                                    </td>
                                    <td  width=""120"">
                                        人员
                                    </td>
                                    <td  width=""120"">
                                        签到时间
                                    </td></tr> ";
        sw.Append(tableHeader);
        int i = 1;
        foreach (DataRow dr in dt.Rows)
        {
            sw.Append(string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>",
                                         i,
                                         dr["DepartmentDisplayName"],
                                         dr["SignDisplayName"],
                                         dr["SignTime"]));
            i += 1;
        }
        sw.Append("</table>");


        byte[] bs = System.Text.Encoding.GetEncoding("utf-8").GetBytes(sw.ToString());

        byte[] outer = new byte[bs.Length + 3];
        outer[0] = (byte)0xef;
        outer[1] = (byte)0xbb;
        outer[2] = (byte)0xbf;
        Array.Copy(bs, 0, outer, 3, bs.Length);
        Response.BinaryWrite(outer);
        Response.Flush();
        Response.End();

    }

    protected void Pager_PreRender(object sender, EventArgs e)
    {
        BindData();

    }
}
